﻿module Sustainalytics.CompanyOverview {

    import GCCS = Sustainalytics.GCCS;

    export class CompanyOverviewController {

        // current company

        public companyNameQueryString: string;
        public company: Company.CorporateDataExtendedDto;

        constructor(
            public $scope: ng.IScope,
            private $q: ng.IQService,
            private $routeParams: ng.route.IRouteParamsService,
            private CompanyService: Company.ICompanyService,
            private $modal: Common.IModal) {

            var ctx = this;
            $scope['companyOverview'] = ctx;

            ctx.init();
        }

        // init controller data
        private init(): ng.IPromise<any> {
            var ctx = this;

            // get company name from route params
            var companyName: string = ctx.$routeParams['companyName'];
            ctx.companyNameQueryString = companyName;

            // get company by name
            var companyPromise = ctx.CompanyService.getByNameUrlFriendly(companyName, true);

            var def = ctx.$q.defer();

            companyPromise
                .success(function (company) {
                    ctx.company = company;
                    console.log(company);
                    def.resolve();
                })
                .error(function (err) {
                    def.reject(err);
                });

            return def.promise;
        }

        public openSavePortfolio = function () {
            var ctx = this;
            var modalInstance = ctx.$modal.open(
                {
                    templateUrl: '/Views/Research/Common/savePortfolio_dialog.tpl.html',
                    controller: 'CompanyAddToPortfolioController',
                    size: 'lg',
                    backdrop: 'static',
                    resolve: {
                        companyId: function () {
                            return ctx.company.id;
                        },
                        companyText: function () {
                            var companyText = ctx.company.companyName;
                            if (ctx.company.exchangeCode != null && ctx.company.ticker != null)
                                companyText += " (" + ctx.company.exchangeCode + ":" + ctx.company.ticker + ")";
                            return companyText;
                        }
                    }
                });

            modalInstance.result.then((item) => {
                //$scope.currentFilter.name = item.name;
                //$scope.currentFilter.shared = item.shared;
                //setIsModified(false);
            });
        };
    }    


    angular.module('Sustainalytics.CompanyOverview').controller('CompanyOverviewController', CompanyOverviewController);
} 